package com.wontube.web.i18n;

import java.util.List;
import java.util.Locale;

import freemarker.template.SimpleScalar;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;

/**
 * freemarker i18n 相关的method
 * @author tanggf(2012-4-10)
 *
 */
public class I18nMethod implements TemplateMethodModelEx{
	private String bundleName = null;
	private Locale locale = null;
	
	public I18nMethod(String bundleName){
		this.bundleName = bundleName;
	}
	public I18nMethod(String bundleName, Locale locale){
		this.bundleName = bundleName;
		this.locale = locale;	
	}	

	@Override
	public Object exec(@SuppressWarnings("rawtypes") List args) throws TemplateModelException {
		String result = null;
		if(args.size() > 0){
			String key = args.get(0).toString();			
			String value = I18nManager.getMessage(key, this.bundleName, this.locale);
			if(value != null){
				result = value;
			}
		}	
		return new SimpleScalar(result);
	}

}
